import React from 'react'
import LocalDate from '@/utils/localDate.js'
export default class time extends React.Component {
  state = {
    timeouter: null,
    timer: null,
  }
  frushTime = () => {
    if (this.state.timeouter) {
      clearTimeout(this.state.timeouter)
    }
    this.setState({
      timer: new LocalDate().format('yyyy-MM-dd HH:mm:ss'),
      timeouter: setTimeout(() => this.frushTime(), 1000),
    })
  }
  componentDidMount() {
    this.frushTime()
  }
  componentWillUnmount() {
    //解决Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method
    //有可能还在执行定时任务，但是组件却已经销毁了，所以必须清除正在执行的定时任务
    clearTimeout(this.state.timeouter)
  }
  render() {
    return <span>{this.state.timer}</span>
  }
}
